import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../controllers/contract_controller.dart';
import '../model/check_in.dart';

class CheckInWidget extends StatelessWidget {
  const CheckInWidget({super.key});

  @override
  Widget build(BuildContext context) {
    final ContractController controller = Get.find();

    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.h),
      child: Obx(() {
        final CheckIn? checkInData = controller.checkIn.value;

        if (checkInData == null) {
          return _buildLoadingWidget();
        }

        return _buildCheckInInterface(checkInData);
      }),
    );
  }

  Widget _buildLoadingWidget() {
    return Container(
      height: 120.h,
      decoration: BoxDecoration(
        color: const Color(0xFF0A0E27),
        borderRadius: BorderRadius.circular(12.r),
        border: Border.all(color: const Color(0xFF00FFFF), width: 2),
      ),
      child: const Center(
        child: CircularProgressIndicator(
          valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF00FFFF)),
        ),
      ),
    );
  }

  Widget _buildCheckInInterface(CheckIn checkInData) {
    return Container(
      height: 120.h,
      decoration: BoxDecoration(
        color: const Color(0xFF0A0E27),
        borderRadius: BorderRadius.circular(12.r),
        border: Border.all(color: const Color(0xFF00FFFF), width: 2),
        boxShadow: [
          BoxShadow(
            color: const Color(0xFF00FFFF).withOpacity(0.3),
            blurRadius: 20,
            spreadRadius: 5,
          ),
        ],
      ),
      child: Padding(
        padding: EdgeInsets.all(16.w),
        child: Row(
          children:
              checkInData.details.asMap().entries.map((entry) {
                final index = entry.key;
                final detail = entry.value;
                final isCurrentDay = _isCurrentDay(detail, checkInData);
                final isCompleted = detail.is_checkin == 1;

                return Expanded(
                  child: _buildDaySection(
                    dayNumber: index + 1,
                    detail: detail,
                    isCurrentDay: isCurrentDay,
                    isCompleted: isCompleted,
                  ),
                );
              }).toList(),
        ),
      ),
    );
  }

  Widget _buildDaySection({
    required int dayNumber,
    required CheckInDetail detail,
    required bool isCurrentDay,
    required bool isCompleted,
  }) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        // 奖励图标
        _buildRewardIcon(isCurrentDay, isCompleted),

        // 天数标签
        Text(
          'DAY $dayNumber',
          style: TextStyle(
            color: isCurrentDay ? const Color(0xFFFFD700) : Colors.white,
            fontSize: 10.sp,
            fontWeight: FontWeight.bold,
          ),
        ),

        // 奖励值
        Text(
          '${detail.hashrate.toStringAsFixed(1)} Gh/s',
          style: TextStyle(
            color: const Color(0xFF00FFFF),
            fontSize: 9.sp,
            fontWeight: FontWeight.w600,
          ),
        ),

        // 持续时间
        Text(
          '24 hours',
          style: TextStyle(color: Colors.white70, fontSize: 8.sp),
        ),
      ],
    );
  }

  Widget _buildRewardIcon(bool isCurrentDay, bool isCompleted) {
    if (isCurrentDay) {
      if (!isCompleted) {
        // 当前可签到日 - 金色礼物盒
        return Container(
          width: 24.w,
          height: 24.h,
          decoration: BoxDecoration(
            color: const Color(0xFFFFD700),
            borderRadius: BorderRadius.circular(4.r),
          ),
          child: Icon(
            Icons.card_giftcard,
            color: const Color(0xFF0A0E27),
            size: 16.sp,
          ),
        );
      } else {
        return Container(
          width: 24.w,
          height: 24.h,
          decoration: BoxDecoration(
            color: const Color(0xFFFFD700),
            borderRadius: BorderRadius.circular(4.r),
          ),
          child: Icon(
            Icons.card_giftcard,
            color: const Color(0xFF0A0E27),
            size: 16.sp,
          ),
        );
      }
    } else if (isCompleted) {
      // 已签到 - 带勾的比特币图标
      return Container(
        width: 24.w,
        height: 24.h,
        decoration: BoxDecoration(
          color: const Color(0xFF00FFFF),
          borderRadius: BorderRadius.circular(12.r),
        ),
        child: Stack(
          children: [
            Center(
              child: Text(
                '₿',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 12.sp,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            Positioned(
              right: 2,
              bottom: 2,
              child: Container(
                width: 8.w,
                height: 8.h,
                decoration: const BoxDecoration(
                  color: Color(0xFF00FF00),
                  shape: BoxShape.circle,
                ),
                child: Icon(Icons.check, color: Colors.white, size: 6.sp),
              ),
            ),
          ],
        ),
      );
    } else {
      // 未到签到日 - 普通比特币图标
      return Container(
        width: 24.w,
        height: 24.h,
        decoration: BoxDecoration(
          color: const Color(0xFF00FFFF),
          borderRadius: BorderRadius.circular(12.r),
        ),
        child: Center(
          child: Text(
            '₿',
            style: TextStyle(
              color: Colors.white,
              fontSize: 12.sp,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      );
    }
  }

  bool _isCurrentDay(CheckInDetail detail, CheckIn checkInData) {
    // 判断是否为当前可签到日
    // 这里可以根据业务逻辑调整判断条件
    return detail.is_checkin == 0 &&
        detail.checkin_date == checkInData.current_date;
  }
}
